package com.nervenets.shopping.controller.common.params;

import com.nervenets.general.jwt.util.JwtUtils;
import com.nervenets.general.web.params.EntityPagingParams;
import com.nervenets.shopping.enumeration.UnionType;
import com.nervenets.shopping.hibernate.domain.common.Fav;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Setter
@Getter
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class FavPagingParams extends EntityPagingParams<Fav> {
    @ApiModelProperty(value = "关联类型")
    private UnionType unionType;
    @ApiModelProperty(value = "目标用户ID，若取自己的，不传")
    private long targetUserId;

    @Override
    public void generateSpecification(List<Predicate> predicates, Root<Fav> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        addEqualsPredicate(root.get("type"), unionType, null != unionType);
        if (0 == targetUserId) this.targetUserId = JwtUtils.getUser().getId();
        addEqualsPredicate(root.get("createBy"), targetUserId, true);
    }
}
